package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlkzhhljlbBaDetailVo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlkzhhljlbBsInfoVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlkzhhlbgBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlkzhhlbgBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlCjlkzhhlbgBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlCjlkzhhlbgBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlCjlkzhhlbgBsInfoService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 矿质混合料配合组成试验检测报告Service业务层处理
 *
 * @author tll
 * @date 2024-10-15
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlkzhhlbgBsInfoServiceImpl implements IReportJlCjlkzhhlbgBsInfoService {

    private final ReportJlCjlkzhhlbgBsInfoMapper baseMapper;

    /**
     * 查询矿质混合料配合组成试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportJlCjlkzhhlbgBsInfoVo queryById(Long excelId){
        ReportJlCjlkzhhlbgBsInfoVo vo = baseMapper.selectVoById(excelId);

        Map<String, String> clmcObj = new HashMap<>();
        Map<String, BigDecimal> cpblObj = new HashMap<>();
        List<String> skccList = new ArrayList<>();
        List<BigDecimal> ljsyList = new ArrayList<>();
        List<BigDecimal> tgbflList = new ArrayList<>();
        List<String> gdzList = new ArrayList<>();

        // 查询报告对应的矿质报表
        ReportJlCjlkzhhljlbBsInfoVo infoVo = baseMapper.getKzjlbInfo(excelId);
        if (ObjectUtils.isNotEmpty(infoVo)) {
            clmcObj.put("cailmc1", infoVo.getCailmc1());
            clmcObj.put("cailmc2", infoVo.getCailmc2());
            clmcObj.put("cailmc3", infoVo.getCailmc3());
            clmcObj.put("cailmc4", infoVo.getCailmc4());
            clmcObj.put("cailmc5", infoVo.getCailmc5());
            clmcObj.put("cailmc6", infoVo.getCailmc6());
            clmcObj.put("cailmc7", infoVo.getCailmc7());
            clmcObj.put("cailmc8", infoVo.getCailmc8());
            clmcObj.put("cailmc9", infoVo.getCailmc9());
            clmcObj.put("cailmc10", infoVo.getCailmc10());

            cpblObj.put("chanpb1", infoVo.getChanpb1());
            cpblObj.put("chanpb2", infoVo.getChanpb2());
            cpblObj.put("chanpb3", infoVo.getChanpb3());
            cpblObj.put("chanpb4", infoVo.getChanpb4());
            cpblObj.put("chanpb5", infoVo.getChanpb5());
            cpblObj.put("chanpb6", infoVo.getChanpb6());
            cpblObj.put("chanpb7", infoVo.getChanpb7());
            cpblObj.put("chanpb8", infoVo.getChanpb8());
            cpblObj.put("chanpb9", infoVo.getChanpb9());
            cpblObj.put("chanpb10", infoVo.getChanpb10());
        }
        List<ReportJlCjlkzhhljlbBaDetailVo> detailVo = baseMapper.getKzjlbDetail(excelId);
        if (ObjectUtils.isNotEmpty(detailVo)) {
            for (ReportJlCjlkzhhljlbBaDetailVo de : detailVo) {
                skccList.add(de.getShaikcc());
                ljsyList.add(de.getHecjp());
                if (ObjectUtils.isNotEmpty(de.getHecjp())) {
                    BigDecimal pa = new BigDecimal(100);
                    BigDecimal res = pa.subtract(de.getHecjp());
                    tgbflList.add(res);
                } else {
                    tgbflList.add(null);
                }
                gdzList.add(de.getGuifyq());
            }
        }
        if (ObjectUtils.isNotEmpty(skccList) && skccList.size() < 10) {
            int a = 10 - skccList.size();
            for (int i=0; i<a; i++) {
                skccList.add(null);
                ljsyList.add(null);
                tgbflList.add(null);
                gdzList.add(null);
            }
        }
        vo.setClmcObj(clmcObj);
        vo.setCpblObj(cpblObj);
        vo.setSkccList(skccList);
        vo.setLjsyList(ljsyList);
        vo.setTgbflList(tgbflList);
        vo.setGdzList(gdzList);
        return vo;
    }

    /**
     * 查询矿质混合料配合组成试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportJlCjlkzhhlbgBsInfoVo> queryPageList(ReportJlCjlkzhhlbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlkzhhlbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlkzhhlbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询矿质混合料配合组成试验检测报告列表
     */
    @Override
    public List<ReportJlCjlkzhhlbgBsInfoVo> queryList(ReportJlCjlkzhhlbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlCjlkzhhlbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlkzhhlbgBsInfo> buildQueryWrapper(ReportJlCjlkzhhlbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlkzhhlbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlCjlkzhhlbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlCjlkzhhlbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlCjlkzhhlbgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlCjlkzhhlbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlCjlkzhhlbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlCjlkzhhlbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlCjlkzhhlbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlCjlkzhhlbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlCjlkzhhlbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlCjlkzhhlbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlCjlkzhhlbgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlCjlkzhhlbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlCjlkzhhlbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlCjlkzhhlbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlCjlkzhhlbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlCjlkzhhlbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlCjlkzhhlbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlCjlkzhhlbgBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlCjlkzhhlbgBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlCjlkzhhlbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportJlCjlkzhhlbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportJlCjlkzhhlbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlCjlkzhhlbgBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlCjlkzhhlbgBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlCjlkzhhlbgBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlCjlkzhhlbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getRefExcelId() != null, ReportJlCjlkzhhlbgBsInfo::getRefExcelId, bo.getRefExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlCjlkzhhlbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportJlCjlkzhhlbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlCjlkzhhlbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlCjlkzhhlbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiel()), ReportJlCjlkzhhlbgBsInfo::getJiel, bo.getJiel());
        return lqw;
    }

    /**
     * 新增矿质混合料配合组成试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportJlCjlkzhhlbgBsInfoBo bo) {
        ReportJlCjlkzhhlbgBsInfo add = BeanUtil.toBean(bo, ReportJlCjlkzhhlbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改矿质混合料配合组成试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportJlCjlkzhhlbgBsInfoBo bo) {
        ReportJlCjlkzhhlbgBsInfo update = BeanUtil.toBean(bo, ReportJlCjlkzhhlbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlkzhhlbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除矿质混合料配合组成试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
